﻿ (function ($) {
            $.fn.prettySelect = function (options) {
                var opts = $.extend({}, $.fn.prettySelect.defaults, options);
                return this.each(function () {
                    var manager = new PrettySelectManager($(this));
                    manager.Setup();
                    if (opts.wrapperClass.length > 0)
                        $(opts.wrapperClass.split(' ')).each(function () { manager.UI.PrettyWrapper.addClass(this); });
                });
            };
            $.fn.prettySelect.defaults = {
                wrapperClass: ""
            };

            function PrettySelectManager(element) {

                this.UI = new Object();
                this.UI.Original = $(element);

                this.Items = new Array();

                if (this.UI.Original.data('prettyselectmanager'))
                    return;

            };

            PrettySelectManager.prototype.Setup = function () {

                this.Utility.AddStyleDeclarationIfNedded();

                this.UI.PrettyWrapper = $('<ul class="prettySelect" />');
                var $manager = this;
                this.UI.Original.find('input:radio, input:checkbox').each(function () {
                    var item = new PrettySelectItem($(this), $manager.UI.Original.find('label[for=' + $(this).attr('id') + ']'));
                    $manager.UI.PrettyWrapper.append(item.BuildPrettyItem());
                    $manager.Items.push(item);
                    item.AttachClick(function () {
                        if (item.__togllableSelect)
                            item.SetState(!item.Checked);
                        else {
                            item.SetState(true);
                            $($manager.Items).each(function () { this.RefreshState(); });
                        }
                    });
                });
                this.UI.Original.hide();
                this.UI.Original.after(this.UI.PrettyWrapper);
            };
            function PrettySelectItem(input, label) {
                this.UI = new Object();
                this.UI.Input = $(input);
                this.UI.Label = $(label);

                this.Checked = this.UI.Input.is(':checked');
                this.Text = this.UI.Label.text();

                this.__togllableSelect = this.UI.Input.is(':checkbox');
            };

            PrettySelectItem.prototype.AttachClick = function (callback) {
                this.UI.Wrapper.click(callback);
            };
            PrettySelectItem.prototype.RefreshState = function () {
                if (this.UI.Input.prop('checked') != this.Checked)
                    this.SetState(this.UI.Input.prop('checked'));
            };
            PrettySelectItem.prototype.SetState = function (state) {
                this.UI.Input.prop('checked', state);
                this.UI.Input.trigger('change');
                this.Checked = state;
                if (this.Checked && !this.UI.Wrapper.hasClass('selected'))
                    this.UI.Wrapper.addClass('selected');
                if (!this.Checked && this.UI.Wrapper.hasClass('selected'))
                    this.UI.Wrapper.removeClass('selected');
            };
            PrettySelectItem.prototype.BuildPrettyItem = function () {
                this.UI.Wrapper = $('<li />');
                this.UI.Wrapper.text(this.Text);
                if (this.Checked)
                    this.UI.Wrapper.addClass('selected');
                var $item = this;
                return this.UI.Wrapper;
            };

            PrettySelectManager.prototype.Utility = {
                AddStyleDeclarationIfNedded: function () {
                    if ($('head').find('style#prettySelectStyle').length > 0) return;
                    $('<style type="text/css" id="prettySelectStyle">ul.prettySelect {display: inline-block;cursor:pointer}ul.prettySelect li {background: url("/Nemo/Styles/images/unchecked.gif") no-repeat scroll left center transparent;color: #85C226;cursor: pointer;display: inline-block;list-style-type: none;margin-right: 20px;padding-left: 17px;}ul.prettySelect li.selected {background-image: url("/Nemo/Styles/images/checked.gif");}</style>')
                        .appendTo('head');
                }
            };
        }(jQuery));